package com.i2bt.app.friend;

import com.i2bt.common.AndroidRestContract;
import com.i2bt.common.Contract;
import com.i2bt.common.UserConfig;

import com.i2bt.R;
import com.i2bt.app.BaseActivity;
import com.i2bt.app.friend.model.FriendAddJsonHandle;
import com.i2bt.bean.FriendBean;
import com.i2bt.util.DateFormatUtil;
import com.i2bt.util.FriendMessageUtil;

import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

public class FriendAddActivity extends BaseActivity {
	
//	private List<FriendInfoBean> friendSearchInfoList;
	
	private EditText searchEdit;		//搜索输入框
	
	private FriendAddJsonHandle friendAddJsonHandle;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_friend_add);
		
		logic();
		initView();
	}
	
	@Override
	protected void logic() {
//		friendSearchInfoList = new ArrayList<FriendInfoBean>();
		if (friendAddJsonHandle == null) {
			friendAddJsonHandle = new FriendAddJsonHandle(this);
		}
	}

	@Override
	protected void initView() {
		
		searchEdit = (EditText) findViewById(R.id.friend_add_edit);
		
		Button searchBtn = (Button) findViewById(R.id.friend_add_btn);
		searchBtn.setOnClickListener(this);
		
		
//		FriendSearchListAdapter friendSearchListAdapter = new
//				FriendSearchListAdapter(this, friendSearchInfoList);
//		
//		ListView searchResultListView = (ListView) findViewById(
//				R.id.friend_search_result_listview);
//		searchResultListView.setAdapter(friendSearchListAdapter);
	}

	@Override
	public void onClick(View v) {
		String addFriendName = searchEdit.getEditableText().toString();
		if (!addFriendName.trim().equals("")) {
			//打包关注好友的bean
			FriendBean friendBean = new FriendBean();
			friendBean.setAct(AndroidRestContract.ADD_FRIEND);
			friendBean.setDate(DateFormatUtil.getDateFormat());
			friendBean.setFlag(FriendMessageUtil.REQUEST);
			friendBean.setLongTimeFlag(System.currentTimeMillis() + "");
			friendBean.setReceiver(addFriendName);
			friendBean.setSender(UserConfig.userName);
			FriendMessageUtil.messageContentEecoded(friendBean);
			friendAddJsonHandle.postTo(AndroidRestContract.REST_FRIEND, friendBean);
		}
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		boolean result = super.onCreateOptionsMenu(menu);
		//设置后退键
		setActionBarBack(Contract.TITLE_FRIEND_ADD_NAME);
		return result;
	}

	@Override
	public void backHomeOperate() {
		
	}
	
}
